package com.pic.mgt.monitor.parser;

import java.io.File;

import org.apache.commons.lang.StringUtils;

import com.pic.mgt.monitor.NoTargetFileException;
import com.pic.mgt.monitor.WrongUnitFormatException;

public  class BusinessTargetFileParser extends FormatParser {
	private String confirmationFilenameEndWith;
	
	@Override
	/**
	 * 分析判斷對應的 Target File 是否存在，若存在則會回傳對應的 Target File。
	 */
	public File parse(Object _confirmationFile)
								throws WrongUnitFormatException {
		// 確認是否為 File 型態
		// throws WrongUnitFormatException
		super.checkObjectType(_confirmationFile, File.class);
		
		File confirmationFile = (File)_confirmationFile;
		
		String targetFileStr = StringUtils.removeEnd(
									confirmationFile.getAbsolutePath(),
									confirmationFilenameEndWith);		
		File targetFile = new File(targetFileStr);
		
		if (!targetFile.exists()) {
			throw new NoTargetFileException(
								"Target file '" + targetFile.getName() + "' doesn't exist.");
		}
		
		return targetFile;
	}

	// --- getter and setter ---
	
	public void setConfirmationFilenameEndWith(String confirmationFilenameEndWith) {
		this.confirmationFilenameEndWith = confirmationFilenameEndWith;
	}
}
